package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.AdminCreateBo;
import com.cskaoyan.bean.bo.AdminParam;
import com.cskaoyan.bean.bo.BaseParam;
import com.cskaoyan.bean.bo.RoleParam;
import com.cskaoyan.bean.pojo.MallRole;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.RoleService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: Fengyi
 * @description: 角色管理
 * @time: 2021/7/8 20:22
 */
@RequestMapping("admin/role")
@RestController
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * @Author fengyi
     * @description: 返回所有管理员角色list
     * @Date 2021/7/9 8:41
     * url: http://localhost:8083/admin/role/options
     */
    @RequestMapping("options")
    public BaseRespVo getRoles() {
        List<SystemRoleVo> systemRoleVos = roleService.queryRoles();
        return BaseRespVo.ok(systemRoleVos);
    }

    /**
     * @Author fengyi
     * @description: 分页显示role
     * @Date 14:39 2021/7/9
     * url:http://localhost:8083/admin/role/list
     */
    @RequestMapping("list")
    @RequiresPermissions("admin:role:list")
    public BaseRespVo roleList(RoleParam roleParam) {
        BaseData<MallRole> roles = roleService.queryAllRole(roleParam);
        return BaseRespVo.ok(roles);
    }

    /**
     * @Author fengyi
     * @description: 创建role
     * @Date 16:16 2021/7/9
     * url: http://localhost:8083/admin/role/create
     */
    @RequestMapping("create")
    @RequiresPermissions("admin:role:create")
    public BaseRespVo createRole(@RequestBody @Validated  MallRole mallRole, BindingResult bindingResult) {

        if(bindingResult.hasFieldErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String message = fieldError.getDefaultMessage();
            return BaseRespVo.fail(message);
        }

        Date date = new Date();
        mallRole.setAddTime(date);
        mallRole.setUpdateTime(date);

        int code = roleService.addRole(mallRole);

        if(code == -1) {
            //有重名
            return BaseRespVo.fail("用户名重复");
        }
        mallRole.setId(code);

        return BaseRespVo.ok(mallRole);
    }

    /**
     * @Author fengyi
     * @description: 更新role
     * @Date 16:32 2021/7/9
     * url: http://localhost:8083/admin/role/update
     */
    @RequestMapping("update")
    @RequiresPermissions("admin:role:update")
    public BaseRespVo updateRole(@RequestBody @Validated MallRole mallRole,
                                 BindingResult bindingResult) {
        if(mallRole.getId() == 1) {
            return BaseRespVo.fail("不能修改超级管理员！");
        }
        if(bindingResult.hasFieldErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String message = fieldError.getDefaultMessage();
            return BaseRespVo.fail(message);
        }

        //更新时间
        Date date = new Date();
        mallRole.setUpdateTime(date);

        int code = roleService.updateRole(mallRole);

        if(code == -1) {
            //有重名
            return BaseRespVo.fail("用户名重复");
        }

        return BaseRespVo.ok();
    }


    /**
     * @Author fengyi
     * @description: 逻辑删除role
     * @Date 16:52 2021/7/9
     * url: http://localhost:8083/admin/role/delete
     */
    @RequestMapping("delete")
    @RequiresPermissions("admin:role:delete")
    public BaseRespVo deleteRole(@RequestBody MallRole mallRole) {
        if(mallRole.getId() == 1) {
            return BaseRespVo.fail("不能删除超级管理员！");
        }
        Integer status = roleService.deleteRole(mallRole);
        if (status == 0) {
            return BaseRespVo.fail("角色被用户引用，不能被删除");
        }
        return BaseRespVo.ok();
    }

    /**
     * @Author fengyi
     * @description: 管理员角色授权
     * @Date 14:13 2021/7/10
     * url: http://localhost:8083/admin/role/permissions
     */
//
    @GetMapping("permissions")
    @RequiresPermissions("admin:role:permissions")
    public String getPermissions(Integer roleId) {
        String str = roleService.getPermissions(roleId);
        return str;
    }

    @PostMapping("permissions")
    @RequiresPermissions("admin:role:permissions")
    public BaseRespVo getPermissions(@RequestBody PermissionVO permissionVO) {

        if(permissionVO.getRoleId() == 1) {
            return BaseRespVo.fail("不能修改超级管理员权限！");
        }
        roleService.updatePermissions(permissionVO);
        roleService.updateOrInsertRolePermissions(permissionVO);
        return BaseRespVo.ok();
    }
}
